<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <!-- 用一个行列布局 -->
        <el-row
          type="flex"
          justify="space-between"
          align="middle"
          style="height: 40px"
        >
          <el-col :span="20">
            <svg-icon icon-class="home" /><span>江苏传智播客教育科技股份有限公司</span>
          </el-col>
          <el-col :span="4">
            <el-row type="flex" justify="end">
              <!-- 两个内容 -->
              <el-col>负责人</el-col>
              <!-- 下拉菜单 element -->
              <el-col>
                <el-dropdown>
                  <span> 操作<i class="el-icon-arrow-down" /> </span>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item
                      @click.native="hAdd('')"
                    >添加子部门</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>
      </el-card>
      <el-card>
        <el-tree :data="list" node-key="id" default-expand-all>
          <template slot-scope="{ data }">
            <el-row
              type="flex"
              justify="space-between"
              align="middle"
              style="height: 40px; width: 100%"
            >
              <el-col :span="20">
                <svg-icon icon-class="home" /><span>{{ data.name }}</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <!-- 两个内容 -->
                  <el-col>{{ data.manager }}</el-col>
                  <!-- 下拉菜单 element -->
                  <el-col>
                    <el-dropdown>
                      <span> 操作<i class="el-icon-arrow-down" /> </span>
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item
                          @click.native="hAdd(data.id)"
                        >添加子部门</el-dropdown-item>
                        <el-dropdown-item
                          @click.native="hEdit(data.id)"
                        >编辑部门</el-dropdown-item>
                        <el-dropdown-item
                          @click.native="hDel(data)"
                        >删除部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-tree>
      </el-card>
    </div>
    <!-- dialog弹出框 -->
    <el-dialog
      close-on-click-modal
      close-on-press-escape
      :title="isEdit ? '编辑部门' : '新增部门'"
      :visible.sync="showDialog"
      @close="hClose1"
    >
      <deptDialogVue
        ref="deptDialogVueRef"
        :valid-list="validList"
        :is-edit="isEdit"
        :pid="curId"
        @close="hClose"
      />
    </el-dialog>
  </div>
</template>

<script>
import { getTree } from '@/utils/getTree'
import { getdepartments, delDepartments } from '@/api/department'
import deptDialogVue from './deptDialog.vue'
export default {
  components: {
    deptDialogVue
  },
  data() {
    return {
      // 控制是否编辑部门
      isEdit: '',
      // 传入子组件的id
      curId: '',
      // 控制dialog隐藏显示
      showDialog: false,
      // 依赖一份树形数据
      list: [],
      defaultProps: {
        children: 'children',
        label: 'name'
      },
      // 传给子组件需要校验的数据
      validList: []
    }
  },
  created() {
    // 获取部门列表
    this.initDepartment()
  },
  methods: {
    // 删除部门
    hDel(data) {
      if (data.children.length !== 0) { return this.$message.warning('带有子级的部门不允许删除哦') }
      this.$confirm('此操作会删除部门，是否继续？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async() => {
          const res = await delDepartments(data.id)
          // 删除成功提示用户
          this.$message.success(res.message)
          // 重新获取部门列表
          this.initDepartment()
        })
        .catch(() => {})
    },
    // 关闭dialog清空表单数据
    hClose1() {
      this.$nextTick(() => {
        this.$refs.deptDialogVueRef.resetForm()
      })
    },
    // 新增 | 编辑 部门成功
    hClose() {
      // 关闭弹窗
      this.showDialog = false
      // 重新获取数据
      this.initDepartment()
    },
    // 添加子部门按钮
    hAdd(id) {
      this.showDialog = true
      this.curId = id
      this.isEdit = false
    },
    // 编辑部门
    hEdit(id) {
      this.showDialog = true
      this.isEdit = true
      this.curId = id
      // 每点击一次编辑部门 重新发送请求
      // $nextTick(回调) 回调函数内的代码会等到dom渲染完再执行
      this.$nextTick(() => {
        this.$refs.deptDialogVueRef.refDepart()
      })
    },
    // 获取部门列表
    async initDepartment() {
      try {
        const res = await getdepartments()
        res.data.depts.shift()
        // 存需要传给子组件校验的数据
        this.list = getTree(res.data.depts)
        this.validList = res.data.depts.map((ele) => {
          return {
            id: ele.id,
            name: ele.name,
            pid: ele.pid,
            code: ele.code
          }
        })
      } catch (err) {
        console.log(err)
      }
    }
  }
}
</script>
